//
//  taoCanVC.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "taoCanVC.h"

@interface taoCanVC ()

@end

@implementation taoCanVC
-(void)dealloc{
    [_TaoCanArray release];
    [_myCollectionView release];
    [_mod release];
    [_name release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self creatCollectionView];
    [[TaoCanAnLiDataBaseHandle shareDataBase]openDB];
    self.TaoCanArray = [[TaoCanAnLiDataBaseHandle shareDataBase]selectAllTao];
    NSLog(@"~~~~~~~~%@",self.TaoCanArray);
    
}
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *viewLayout = [[UICollectionViewFlowLayout alloc] init];
    viewLayout.itemSize = CGSizeMake((375-60)/2*kWidth/375,120*kHeight/667);
    viewLayout.sectionInset = UIEdgeInsetsMake(20*kWidth/375, 20*kHeight/667, 20*kWidth/375, 20*kHeight/667);
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, 375*kWidth/375, (667-49)*kHeight/667) collectionViewLayout:viewLayout];
    
    self.myCollectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    
#pragma mark --注册了，就不需要判断是否为空了
    //注册了，就不需要判断是否为空了
    [self.myCollectionView registerClass:[TaoCanCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}
#pragma mark 实现UICollectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.TaoCanArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TaoCanCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    self.mod = [self.TaoCanArray objectAtIndex:indexPath.row];
    NSString *cover_path = self.mod.cover_path;
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:cover_path]];
    cell.titleLabel.text = self.mod.title;
    cell.nameLabel.text = self.mod.name;
    
    //3D效果
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{   [[TaoCanAnLiDataBaseHandle shareDataBase]openDB];
    TaoCanCollectionViewCell *acell = (TaoCanCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    self.name = acell.nameLabel.text;
    UIAlertView *alertVC = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"真的要删除嚒嚒" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertVC show];
    NSLog(@"~~~~~~~~~~~~!!!!%ld",(unsigned long)self.TaoCanArray.count);

    NSString *name = acell.nameLabel.text;
    [[TaoCanAnLiDataBaseHandle shareDataBase]deleteTableByName:name];
//    [[TaoCanAnLiDataBaseHandle shareDataBase]deleteTable:indexPath.row+1];


    [self.myCollectionView reloadData];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[TaoCanAnLiDataBaseHandle shareDataBase]deleteTableByName:self.name];
        [self viewDidLoad];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
